<?php
declare (strict_types=1);

namespace app\common\library\upload\driver;

use think\facade\Cache;
use app\common\library\upload\Driver;

class Local extends Driver
{

    private $config = [];

    public function __construct()
    {
        $this->config = config('system.upload');
    }

    /**
     * @title 普通文件上传 (大小不超过5MB)
     * @param string $file 上传的input框的name值
     * @param string $save_name 保存的文件名,不传默认生成随机文件名
     * @return array
     * *@author wanghao
     */
    public function upload(string $file, string $save_name = ''): array
    {
        if (empty($this->config['path'])) {
            $this->error = '上传路径不存在';
            return [];
        }
        if (!$this->checkFile($file)) return [];
        //转小写
        if ($save_name != '') {
            $ext = strtolower(substr(strrchr($save_name, '.'), 1));
        } else {
            $ext = strtolower(substr(strrchr($_FILES[$file]['name'], '.'), 1));
        }

        if (!$this->checkExt($ext)) return [];
        //保存的文件名称
        if ($save_name == '') {
            $save_name = uniqid() . '.' . $ext;
        } else {
            $save_name = strtolower($save_name);
        }

        $file_dir = 'upload/' . $ext . '/' . date("Ym/d") . '/';

        if (!is_dir($this->config['path'] . $file_dir)) {
            try {
                mkdir($this->config['path'] . $file_dir, 0777, true);
            } catch (\Exception $exception) {
                $this->error = '目录:' . $this->config['path'] . $file_dir . '创建失败，请检查权限';
                return [];
            }
        }
        $save_file = $this->config['path'] . $file_dir . $save_name; //文件的保存目录
        $result = move_uploaded_file($_FILES[$file]['tmp_name'], $save_file);
        if (!$result) {
            $this->error = '文件上传失败';
            return [];
        }
        if (!empty($this->config['url'])) {
            $url = $this->config['url'] . $file_dir . $save_name;
        } else {
            $url = get_host() . '/' . $file_dir . $save_name;
        }
        return [
            'url' => $url,
            'size' => filesize($save_file),
            'md5' => md5_file($save_file),
        ];
    }

    /**
     * @title 分片上传大文件
     * @param string $file 上传的input框的name值
     * @param string $save_name 保存的文件名
     * @return array
     * @author wanghao
     */
    public function uploadBig($file, $save_name): array
    {
        //获取配置
        if (empty($this->config['path'])) {
            $this->error = '上传路径不存在';
            return [];
        }

        if (!$this->checkFile($file)) return [];
        $save_name = strtolower($save_name);
        $ext = substr(strrchr($save_name, '.'), 1);
        if (!$this->checkExt($ext)) return [];

        $file_dir = 'upload/' . $ext . '/' . date("Ym/d") . '/';

        if (!is_dir($this->config['path'] . $file_dir)) {
            try {
                mkdir($this->config['path'] . $file_dir, 0777, true);
            } catch (\Exception $exception) {
                $this->error = '目录:' . $this->config['path'] . $file_dir . '创建失败，请检查权限';
                return [];
            }
        }

        $save_file = $this->config['path'] . $file_dir . $save_name; //文件的保存目录
        $result = Cache::store('redis')->get(md5($save_name));
        if (!$result && is_file($save_file)) {
            $this->error = '该文件已经存在，请更换保存文件名';
            return [];
        }

        $result = file_put_contents($save_file, file_get_contents($_FILES[$file]['tmp_name']), FILE_APPEND);
        if (!$result) {
            $this->error = '保存失败了，请刷新页面重试';
            return [];
        }
        Cache::store('redis')->set(md5($save_name), $result, 600);
        if (!empty($this->config['url'])) {
            $url = $this->config['url'] . $file_dir . $save_name;
        } else {
            $url = get_host() . '/' . $file_dir . $save_name;
        }
        return [
            'url' => $url,
            'size' => filesize($save_file),
            'md5' => md5_file($save_file),
        ];
    }

}
